package com.roc.web.action.merchantrans;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.CalcuBean;
import com.roc.pojo.Esmidinfo;
import com.roc.pojo.Operators;
import com.roc.pojo.OrgBean;
import com.roc.pojo.TransReport;
import com.roc.service.IPagenitionService;
import com.roc.service.IorgService;
import com.roc.service.ServiceException;
import com.roc.service.transReport.ITransReportService;
import com.roc.util.Constance;
import com.roc.util.constant.ConfigProperties;
import com.roc.util.constant.Constant;
import com.roc.web.action.merinfo.MerinfoChangeAction;
import com.roc.web.action.merinfo.MerinfoChangeSearchCondition;
@Component
@Scope("prototype")
@Namespace("/merchantTrans")
public class MerchantTransQueryAction extends PaginationBaseAction{

	private Logger logger=LogManager.getLogger(MerchantTransQueryAction.class);
	private MerchantTransQuerySearchCondition searchCondition;
	private TransReport transReport;
	private List<CalcuBean> calcuList;
	private Esmidinfo mrinfo;
	private List<OrgBean> orgList=new ArrayList<OrgBean>();
	
	@Autowired
	IPagenitionService pagenitionService;
	@Autowired
	ITransReportService transReportService;
	@Autowired
	IorgService orgService;
	
	@Action(value = "init", results = { @Result(name = "success", location = "/pages/merchantReport/merchantTransQuery.jsp")
    })
	public String init() throws Exception{
		Operators loginOperators = (Operators) ActionContext.getContext().getSession().get(Constant.SESSION_LOGIN_MERCHANT);
	    try{
	    	getRequest().setAttribute("areaList", ConfigProperties.getAreaCodeProperties());
			getRequest().setAttribute("mccTypeList", ConfigProperties.getMccTypeProperties());
			orgList=orgService.listOrgs(loginOperators);
			
		}catch (Exception e) {
		// TODO: handle exception
		addActionError("加载信息失败，请联系客服人员");
		logger.error(e.getMessage(),e);
		}
			
	   return SUCCESS;
    }
	
	@Action(value = "queryMerchantTrans", 
			results = { @Result(name = "success", location = "/pages/merchantReport/merchantTransQuery.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })
	public String queryMerchantTrans() throws Exception{
		 
		Operators loginOperators = (Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		List<TransReport> tranList=new ArrayList<TransReport>();

		try {
			
			init();
			if(Constance.Null2EmptyTrim(searchCondition.getMinDate()).equals("")||Constance.Null2EmptyTrim(searchCondition.getMaxDate()).equals("")){
				getRequest().setAttribute("msg", "请选择时间");
				
				return SUCCESS;
				
			}
			int rows=pagenitionService.count(searchCondition);
			if(rows>0){
				searchRsList=pagenitionService.findAll(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
				
			}
			dealWidth(tranList);
			
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute("tranList", tranList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			
		}catch(ServiceException e){
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}
	
	private void dealWidth(List<TransReport> tranList){
		if(searchRsList.size()>0 && searchRsList!=null){
			
			Iterator iterator=searchRsList.iterator();
			while (iterator.hasNext()) {
				Object[] obj= (Object[])iterator.next();
				TransReport temp=new TransReport();
				temp.setMerchantNo(Constance.Null2EmptyTrim(obj[0]));
				temp.setMerName(Constance.Null2EmptyTrim(obj[1]));
				temp.setMccType(Constance.Null2EmptyTrim(obj[2]));
				temp.setMcc(Constance.Null2EmptyTrim(obj[3]));
				temp.setAreaCode(Constance.Null2EmptyTrim(obj[4]));
				temp.setTotalAmt(Double.parseDouble(Constance.null2Amt(obj[5])));
				temp.setTotalFee(Double.parseDouble(Constance.null2Amt(obj[6])));
				temp.setTotalNum(Long.parseLong(Constance.Null2EmptyTrim(obj[7])));
				temp.setTransDate(Constance.Null2EmptyTrim(obj[8]));
				temp.setAvgAmt(Double.parseDouble(Constance.null2Amt(obj[9])));
				temp.setFeeType(Constance.Null2EmptyTrim(obj[10]));
				temp.setOrgCode(Constance.Null2EmptyTrim(obj[11]));
				tranList.add(temp);
			}
		}
		for(int i=0;i<tranList.size();i++){
			TransReport temp=(TransReport) tranList.get(i);
			temp.setAreaCode( ConfigProperties.getAreaCodeProperties().get(temp.getAreaCode()));
			temp.setMccType(ConfigProperties.getMccTypeProperties().get(temp.getMccType()));
			for(int j=0;j<orgList.size();j++){
				if(temp.getOrgCode().equals(orgList.get(j).getOrg_code())){
					temp.setOrgCode(orgList.get(j).getOrg_name());
				}
			}
			
		}
	}
	
	@Action(value = "transDetail", 
			results = { @Result(name = "success", location = "/pages/merchantReport/transDetail.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })
	public String transDetail(){
		//Operators loginOperators = (Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		
		try{
			transReport=transReportService.queryTransReportById(transReport.getId());
			transReport.setAreaCode( ConfigProperties.getAreaCodeProperties().get(transReport.getAreaCode()));
			transReport.setMccType(ConfigProperties.getMccTypeProperties().get(transReport.getMccType()));
			calcuList=transReportService.queryTrans(transReport.getMerchantNo(), transReport.getTransDate());
			
		}catch(ServiceException e){
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}
	
	@Action(value = "merchantDetail", 
			results = { @Result(name = "success", location = "/pages/merchantReport/merchantDetail.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })
	public String merchantDetail(){
		try{
			//transReport=transReportService.queryTransReportById(transReport.getId());
			mrinfo=transReportService.queryMerinfo(transReport.getMerchantNo());
		}catch(ServiceException e){
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}
	
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		// TODO Auto-generated method stub
		this.setSearchConditionToSubClass(searchCondition);
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		// TODO Auto-generated method stub
		getSession().setAttribute(SEARCH_SESSION_MERCHANT_TRANS_QUERY, searchCondition);
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (BaseSearchCondition) getSession().getAttribute(SEARCH_SESSION_MERCHANT_TRANS_QUERY);
	}

	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return Constance.PAGE_MAX_SIZE+"";
	}

	public MerchantTransQuerySearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(MerchantTransQuerySearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}

	public TransReport getTransReport() {
		return transReport;
	}

	public void setTransReport(TransReport transReport) {
		this.transReport = transReport;
	}

	public List<CalcuBean> getCalcuList() {
		return calcuList;
	}

	public void setCalcuList(List<CalcuBean> calcuList) {
		this.calcuList = calcuList;
	}

	public Esmidinfo getMrinfo() {
		return mrinfo;
	}

	public void setMrinfo(Esmidinfo mrinfo) {
		this.mrinfo = mrinfo;
	}

	public List<OrgBean> getOrgList() {
		return orgList;
	}

	public void setOrgList(List<OrgBean> orgList) {
		this.orgList = orgList;
	}
	
	

}
